﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DevelopTool.Bll.CodeBlls;
using DevelopTool.Bll.ManageMenuBlls;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model.CodeModels;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using Sunny.UI;

namespace DevelopTool.Forms.CodeForms
{
    /// <summary>
    /// 代码管理
    /// </summary>
    public partial class CodeManageForm : BaseUIPage
    {
        private ManageMenuBll manageMenull;

        private CodeBll codeBll;


        public CodeManageForm()
        {
            InitializeComponent();
        }


        private void CodeManageForm_Load(object sender, EventArgs e)
        {
            manageMenull = new ManageMenuBll();
            codeBll = new CodeBll();

            this.manageControl.BaseBll = codeBll;

            LoadManageControl();
        }

        /// <summary>
        /// 加载管理页控件
        /// </summary>
        private void LoadManageControl()
        {
            List<ManageMenu> manageMenus = new List<ManageMenu>();
            LoadingHelper.ShowLoading("加载菜单数据", () =>
            {
                manageMenus = codeBll.GetList().Cast<ManageMenu>().ToList();//查询所有代码信息
            });

            this.manageControl.SetMenuData(manageMenus);
        }


        /// <summary>
        /// 菜单项添加时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdded(object sender, MenuEventArgs e)
        {
            UINavMenu nav = sender as UINavMenu;

            if (e.TreeNode.Name == "TopNode")
            {
                nav.SetNodeSymbol(e.TreeNode, 61641);
            }
            else if (e.TreeNode.Name == "Folder")
            {
                nav.SetNodeSymbol(e.TreeNode, 61564);
            }
            else if (e.TreeNode.Name == "Node")
            {
                nav.SetNodeSymbol(e.TreeNode, 361788);
            }
        }


        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="text"></param>
        private void ManageControl_MenuQuery(object sender, string text)
        {
            Dictionary<string, List<ManageMenu>> dic = new Dictionary<string, List<ManageMenu>>();

            //关联到的节点名
            var nameList = manageMenull.GetIdListByName<Code>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的节点名", nameList);

            var timeList = manageMenull.GetIdListByTime<Code>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的日期", timeList);

            var textList = codeBll.GetListByText(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的代码内容", textList);

            this.manageControl.SetMenuQueryData(dic);
        }


        /// <summary>
        /// 菜单节点双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDoubleClick(object sender, MenuEventArgs e)
        {
            if (e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                Code code = codeBll.GetById(e.ManageMenu.Id);

                this.manageControl.ShowPageForm(new CodeForm( code), code.NodeName, true, code.NodeName + code.Id);
            }
        }


        /// <summary>
        /// 菜单节点拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDrag(object sender, MenuEventArgs e)
        {
            Code code = manageMenull.GetExample<Code>(e.ManageMenu);

            manageMenull.SimpleSave(code);
        }





        /// <summary>
        /// 删除菜单节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDelete(object sender, MenuEventArgs e)
        {
            Code code = manageMenull.GetExample<Code>(e.ManageMenu);

            if (codeBll.Delete(code.Id))
            {
                this.ShowSuccessNotifier($"【{code.NodeName}】已删除");
            }
            else
            {
                e.Cancel = true;
                this.ShowWarningNotifier("不能删除非空目录");
            }
        }


        /// <summary>
        /// 重新加载菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuNeedReLoad(object sender, EventArgs e)
        {
            LoadManageControl();
        }




        /// <summary>
        /// 菜单节点重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuRename(object sender, MenuEventArgs e)
        {
            Code code = manageMenull.GetExample<Code>(e.ManageMenu);
            manageMenull.SimpleSave(code);
        }


        /// <summary>
        /// 新建菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdd(object sender, MenuEventArgs e)
        {
            Code code = manageMenull.GetExample<Code>(e.ManageMenu);

            code = manageMenull.SimpleSave(code);
            e.ManageMenu = code;

            this.ShowSuccessNotifier("新建成功");
        }

    }
}
